<?php
namespace app\common\controller;
use think\Controller;

use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class AlicloudAuth extends Controller
{
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Cloudauth Client
     */
    public function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 必填，您的 AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 必填，您的 AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/Cloudauth
        $config->endpoint = "cloudauth.aliyuncs.com";
        return new Cloudauth($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public function photoVerifyIdcard($realname,$id_card,$photo){
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html
        $config = \config('aliyun_sms'); //获取配置信息
        $accessKeyId = $config['KeyID'];//阿里云短信keyId
        $accessKeySecret = $config['KeySecret'];//阿里云短信keysecret

        $client = $this->createClient($accessKeyId, $accessKeySecret);

        // $client = $this->createClient(getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
        $contrastFaceVerifyRequest = new ContrastFaceVerifyRequest([
            "sceneId" => 1000007639,
            "outerOrderNo" => time().rand(1111,9999),
            "productCode" => "ID_MIN",
            "certType" => "IDENTITY_CARD",
            "certName" => $realname,
            "certNo" => $id_card,
            'faceContrastPicture' => $photo,
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $res = $client->contrastFaceVerifyWithOptions($contrastFaceVerifyRequest, $runtime);
            return $res;
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 如有需要，请打印 error
            Utils::assertAsString($error->message);
        }
    }


}